
clear all
set more off

** Change path to re-run on another computer
cd ""
**


*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
*** 1) Import UNIDO GVA data
*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
insheet using "Other data/INDSTAT/INDSTAT4REV4_28072017162428953.csv", nodouble comma clear


gen check=strlen(isic)
tab check

gen sector="Iron and steel" if isic=="241" |isic=="2410" | isic=="2431"
replace sector="Chemical and petrochemical" if isic=="201" | isic=="202" | isic=="2100"
replace sector="Non-ferrous metals" if isic=="2420" | isic=="2432"
replace sector="Non-metallic minerals" if isic=="2310" | isic=="239"
replace sector="Transport equipment" if isic=="2910" | isic=="2920" | isic=="2930" | isic=="301" | isic=="3020" | isic=="3030" | isic=="3040" | isic=="309"
replace sector="Machinery" if isic=="251" | isic=="2520" | isic=="259" | isic=="2610" | isic=="2620" | isic=="2630" | isic=="2640" | isic=="265" | isic=="2660" | isic=="2670" | isic=="2680" | isic=="2710" | isic=="2720" | isic=="273" | isic=="2740" | isic=="2750" | isic=="2790" | isic=="281" | isic=="282" 
replace sector="Food and tobacco" if isic=="1010" | isic=="1020" | isic=="1030" | isic=="1040" | isic=="1050" | isic=="106" | isic=="107" | isic=="1080" | isic=="110" | isic=="1200"
replace sector="Paper, pulp and print" if isic=="170" | isic=="181" | isic=="1820"
replace sector="Wood and wood products" if isic=="1610" | isic=="162"
replace sector="Textile and leather" if isic=="131" | isic=="139" | isic=="1410" | isic=="1420" | isic=="1430" | isic=="151" | isic=="1520"

drop if sector==""
keep country time value sector
rename time year
duplicates drop
bysort country year sector: egen GVA_nominal=total(value)
drop value
duplicates drop
isid cou year sec

*DEFLATE SERIES AND PUT IN USD
replace country="Korea, Republic of" if country=="Republic of Korea"
merge m:1 cou year using "Other data/ER_GDPdeflator.dta", 
drop if _m==1 // lithuania and some others
drop if _m==2 // countries that dont need

gen temp = deflator2010/exchange_rate if year == 2010
bysort country: egen scalar=total(temp)

gen GVA_real=GVA_nominal/deflator2010
gen GVA_consUS=GVA_real*scalar

keep cou year sect GVA_consUS

replace GVA_cons=. if GVA_cons<=0

tempfile GVAdata
save `GVAdata'


*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
*** 2) Add FEPI data and price data to compute energy costs
*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

use "Energy_prices_imputed(index)", clear

keep isoalpha2code isoalpha3code isonum deflator2010 country OECD year mdummy *consUS *PPPcons *totindex *respindex *resptotindex

merge 1:m country year using "IPOL Fuel type use per sector_agregated"
drop if year<1995
drop if _m==2 // countries that are not contained in price data
drop _m

merge m:1 country year sector using `GVAdata'

**
drop if type=="Biofuels and waste"
** GENERATE ENERGY COSTS by fuel
gen double t_coal=ktoe_extra*1000*coal_consUS_resptotindex  if type=="Coal"
gen double t_electricity=ktoe_extra*1000*electricity_consUS_resptotindex  if type=="Electricity and heat"
gen double t_oil=ktoe_extra*1000*oil_consUS_resptotindex  if type=="Oil products"
gen double t_gas=ktoe_extra*1000*gas_consUS_resptotindex  if type=="Natural gas"

foreach fuel in coal electricity oil gas{
bysort country year sector: egen ecost_`fuel'=total(t_`fuel'), missing
}

bysort country year sector: egen double ktoe_tot=total(ktoe_extra), missing

egen ecost_tot=rowtotal(ecost_coal ecost_electricity ecost_oil ecost_gas), missing

keep country year sector ecost* ktoe_tot GVA coal_consUS_resptotindex electricity_consUS_resptotindex oil_consUS_resptotindex gas_consUS_resptotindex

duplicates drop


br if cou=="Slovakia" & sector=="Iron and steel" // in 2008 hihg cost/gva ratio
replace GVA_consUS=GVA_consUS*10 if cou=="Slovakia" & sector=="Iron and steel" & year==2008

*** produce industry without the above non UNIDO sectors

gen unidoind=1 if sector=="Textile and leather" | sector=="Iron and steel" | sector=="Chemical and petrochemical" | sector=="Non-ferrous metals" | sector=="Non-metallic minerals" | sector=="Transport equipment" | sector=="Machinery" | sector=="Food and tobacco" | sector=="Paper, pulp and print" | sector=="Wood and wood products" 
replace unidoind=. if GVA==.
bysort country year: egen check=count(GVA) if unidoind==1

bysort country year: egen GVA_ind=total(GVA) if unidoind==1 & check==10, missing
bysort country year: egen GVA_ind2=max(GVA_ind)
replace GVA_cons=GVA_ind2 if sector=="Industry Unido"
drop unidoind GVA_ind GVA_ind2 check


gen ecost_GVA=ecost_tot*100/GVA // so in percent
gen euse_GVA=ktoe_tot*1000*1000000/GVA // in toe/mil gva

*Merge with FEPI:
replace sec="Manufacturing" if sec=="Industry Unido"

merge 1:1 country year sector using "VEPL_FEPI_data",keepusing( FEPI_fw2010 VEPL_MER)
drop if _m==2
drop _m

gen ln_euse_GVA=ln(euse_GVA)
gen ln_ecost_GVA=ln(ecost_GVA)


**generate log vepl for regression analysis

g log_VEPL=log(VEPL_MER)

egen id=group(country sector)
xtset id year
set scheme s2manual

**non-unido sectors
drop if sector=="Industry"
drop if sector=="Construction"
drop if sector=="Mining and quarrying"
drop if sector=="Non-specified (industry)"

sum ecost_GVA  euse_GVA 

// there are fewer observations in industry unido, since ktoe is only constructed if complete for all sectors.
tab cou  if sec=="Iron and steel" &  ln_ecost_GVA!=.
tab cou  if sec=="Manufacturing" &  ln_ecost_GVA!=.
br if cou=="Denmark" & (sec=="Manufacturing" | sec=="Iron and steel")


replace sec="All manufacturing" if sec=="Manufacturing"

****Energy costs
************************************

**FE regression 
local i=1
levelsof sector,local(sect)
foreach sec of local sect{
preserve
keep if sector=="`sec'"
di "`sec'"
qui tab year
*xtreg  ln_ecost_GVA FEPI_fw2010 i.year, fe robust 
xtivreg  ln_ecost_GVA (log_VEPL=FEPI_fw2010) i.year, fe vce(cluster id) 
estimates store costfe_`i'
di "`sec'"
*test FEPI_fw2010==1
test log_VEPL==1
local i=`i'+1
restore
}

****Energy use
**********************************

**FE regression
local i=1
levelsof sector,local(sect)
foreach sec of local sect{
preserve
*drop if cou=="Finland"
keep if sector=="`sec'"
di "`sec'"
*xtreg  ln_euse_GVA FEPI_fw2010 i.year, fe robust 
xtivreg  ln_euse_GVA (log_VEPL=FEPI_fw2010) i.year, fe vce(cluster id) 
estimates store usefe_`i'
local i=`i'+1
restore
}

esttab usefe_* ///
, indicate(Year_dummies=*.year) mtitles(`sect')  star(* 0.1 ** 0.05 *** 0.01) nogaps  se(%4.2f) b(%9.2g) r2  drop(_cons)

esttab costfe_* ///
, indicate(Year_dummies=*.year) mtitles(`sect')   star(* 0.1 ** 0.05 *** 0.01) nogaps  p(%4.2f) b(%9.2g) r2  drop(_cons)
exit

esttab usefe_* using  "Graphs/some descriptive statistics/GVA_use/all sect.tex" ///
, indicate(Year_dummies=*.year) mtitles(`sect')  star(* 0.1 ** 0.05 *** 0.01) nogaps  se(%4.2f) b(%9.2g) r2  drop(_cons)  replace

esttab costfe_* using  "Graphs/some descriptive statistics/GVA_cost/all sect.tex" ///
, indicate(Year_dummies=*.year) mtitles(`sect')  star(* 0.1 ** 0.05 *** 0.01) nogaps  se(%4.2f) b(%9.2g) r2  drop(_cons)  replace

